package com.wy.jetpack.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.wy.jetpack.R;
import com.wy.jetpack.viewmodel.VModel;

public class ViewModelActivity extends AppCompatActivity {

    private TextView tv_1, tv_2;
    private Button bt_1, bt_2;
    private VModel vModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vm);

        vModel = new ViewModelProvider(this).get(VModel.class);

        tv_1 = findViewById(R.id.tv_1);
        tv_2 = findViewById(R.id.tv_2);
        bt_1 = findViewById(R.id.bt_1);
        bt_2 = findViewById(R.id.bt_2);

        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vModel.setNum_1(vModel.getNum_1() + 1);
                tv_1.setText(String.valueOf(vModel.getNum_1()));
            }
        });

        bt_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vModel.setNum_2(vModel.getNum_2() + 1);
                tv_2.setText(String.valueOf(vModel.getNum_2()));
            }
        });
    }
}
